<?php
/**
 * +----------------------------------------------------------------------
 * | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
 * +----------------------------------------------------------------------
 * | File name: PlatformRequest.php
 * +----------------------------------------------------------------------
 * | Author: sam
 * +----------------------------------------------------------------------
 * | Description: 请求工具类公共Trait
 * +----------------------------------------------------------------------
 * | Others: 其他内容的说明
 * +----------------------------------------------------------------------
 * | History: 修改历史记录列表，每条修改记录应包含修改日期、修改者及修改内容简述
 * +----------------------------------------------------------------------
 * | 1.Date: 2018/12/27 Author: sam Modification: 创建
 * +----------------------------------------------------------------------
 */

namespace App\Library\Traits;
use App\Constant\Compatible;
use App\Models\RepaySummary;
use GuzzleHttp\Client;
Trait PlatformRequest
{
    /**
     * Description:公共请求方法
     * Author: Sam
     * DateTime: 2019/1/2 15:04
     * @param $url "请求地址"
     * @param $platform  "平台类型 money or common"
     * @param string $method "请求方法"
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function request($url,$platform,$use_type = RepaySummary::DATA_TYPE_ONE,$method = 'POST')
    {
        if($use_type == RepaySummary::DATA_TYPE_ONE){
            $pubMerNo = config('common.'.$platform.'_platform_pub_mer_no');
        }else if(RepaySummary::DATA_TYPE_TWO == $use_type){
            $pubMerNo = config('common.old_'.$platform.'_platform_pub_mer_no');
        }
        $client = new Client(['base_uri'=>$this->platform_uri,'timeout'=>30.0]);
        $this->body = [
            'pubData'=>$this->_pub_data,
            'pubMerNo'=>$pubMerNo,
            'pubSign'=>$this->_pub_sign,
            'pubVersion'=>config('common.'.$platform.'_platform_pub_version'),
        ];

        \Log::debug('request-xinliu:'.json_encode($this->body,JSON_UNESCAPED_UNICODE) . ',url:' . $url);
        $result = $client->request($method,$url,['json'=>$this->body,'verify'=>false]);
        return json_decode($result->getBody()->getContents(), true);
    }
}